package com.example.community.service;

import com.alibaba.fastjson.JSON;
import com.example.community.dto.UserDto;
import com.example.community.mapper.UserMapper;
import com.example.community.model.User;
import com.example.community.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public Long getUserId(String token){
        UserExample userExample = new UserExample();
        userExample.createCriteria().andTokenEqualTo(token);
        return userMapper.selectByExample(userExample).get(0).getId();
    }

    public void updateUser(User user,Long id){
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(id);
        userMapper.updateByExampleSelective(user,userExample);
    }

    public void createUser(User user) throws HttpRequestMethodNotSupportedException {
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andPhoneEqualTo(user.getPhone());
        List<User> users = userMapper.selectByExample(userExample);
        //插入新用户
        if (users.size() == 0){
            user.setGmtCreate(System.currentTimeMillis());
            user.setGmtModified(user.getGmtCreate());
            userMapper.insert(user);
        }else {
            throw new HttpRequestMethodNotSupportedException("用户已存在");
        }
    }

    public void loginUser(User user,String ip) throws HttpRequestMethodNotSupportedException {
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andPhoneEqualTo(user.getPhone())
                .andPasswordEqualTo(user.getPassword());
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() > 0){
            User dbUser = users.get(0);
            User insertUser = new User();
            insertUser.setToken(user.getToken().substring(2));
            insertUser.setIp(ip);
            UserExample userInsertExample = new UserExample();
            userInsertExample.createCriteria()
                    .andPhoneEqualTo(dbUser.getPhone());
            userMapper.updateByExampleSelective(insertUser,userInsertExample);
        }else {
            throw new HttpRequestMethodNotSupportedException("用户不存在或密码错误");
        }
    }

    public UserDto getProfile(Long userId){
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(userId);
        User user = userMapper.selectByPrimaryKey(userId);
        return UserDto.getUserDtoByUser(user);
    }
}
